package com.bq;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.TimeUnit;

public class Test {
    public static void main(String[] args) throws InterruptedException {
//        test1();
//        test2();
//        test3();
        test4();

    }

    /**
     * 抛出异常
     * */
    public static void test1() {
        // 3：队列的容量
        BlockingQueue blockingQueue = new ArrayBlockingQueue<>(3);

        System.out.println(blockingQueue.add("a"));
        System.out.println(blockingQueue.add("b"));
        System.out.println(blockingQueue.add("c"));
        // 检测队首元素
        System.out.println(blockingQueue.element());  // a
        // java.lang.IllegalStateException: Queue full
        //System.out.println(blockingQueue.add("d"));
        System.out.println("===================");
        System.out.println(blockingQueue.remove());// a
        System.out.println(blockingQueue.element()); // b
        System.out.println(blockingQueue.remove());// b
        System.out.println(blockingQueue.remove());// c
        // java.util.NoSuchElementException
        System.out.println(blockingQueue.remove());
    }

    /**
     * 有返回值，不抛出异常
     * false,null
     * */
    public static void test2() {
        BlockingQueue blockingQueue = new ArrayBlockingQueue<>(3);

        System.out.println(blockingQueue.offer("a"));
        System.out.println(blockingQueue.offer("b"));
        System.out.println(blockingQueue.offer("c"));
        System.out.println(blockingQueue.offer("d")); // false
        // 查看队首元素
        System.out.println(blockingQueue.peek());  //a
        System.out.println("==========================");
        System.out.println(blockingQueue.poll());// a
        System.out.println(blockingQueue.poll());// b
        // 查看队首元素
        System.out.println(blockingQueue.element());//c
        System.out.println(blockingQueue.poll());// c
        System.out.println(blockingQueue.poll());// null
    }

    /**
     * 等待、阻塞（一直阻塞）
     * */
    public static void test3() {
        BlockingQueue blockingQueue = new ArrayBlockingQueue(3);

        try {
            blockingQueue.put("a");
            blockingQueue.put("b");
            blockingQueue.put("c");
//            blockingQueue.put("d");// 没有位置了，一直阻塞
            System.out.println(blockingQueue.take());
            System.out.println(blockingQueue.take());
            System.out.println(blockingQueue.take());
            System.out.println(blockingQueue.take());// 没有这个元素，一直等待
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    /**
     * 等待、阻塞（等待超时）
     * */
    public static void test4() throws InterruptedException {
        BlockingQueue blockingQueue = new ArrayBlockingQueue(3);

        System.out.println(blockingQueue.offer("a"));
        System.out.println(blockingQueue.offer("b"));
        System.out.println(blockingQueue.offer("c"));
        System.out.println(blockingQueue.offer("d", 2, TimeUnit.SECONDS)); // 等待2秒就退出

        System.out.println("====================");
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll(2, TimeUnit.SECONDS)); // 等待2秒就退出
    }
}
